package model;

import java.awt.Color;
import java.awt.Rectangle;

public class Spielfeld {
	private SchrittList schritte;
	private int width;
	private int height;

	public Spielfeld(int width, int height) {
		this.schritte = new SchrittList(10, width, height);
		this.width = width;
		this.height = height;

		// Rand einzeichnen
		float breite = 5;
		float dicke = 3;
		// Oben
		for (int i = 0; i <= width; i += breite) {
			addMove(new Schritt(i, 0, i, dicke, i + breite, 0, i + breite,
					dicke, Color.WHITE));
		}
		// Links
		for (int i = 0; i <= height; i += breite) {
			addMove(new Schritt(0, i, dicke, i, 0, i + breite, dicke, i
					+ breite, Color.WHITE));
		}
		// Unten
		for (int i = 0; i <= width; i += breite) {
			addMove(new Schritt(i, this.height, i, this.height - dicke, i
					+ breite, this.height, i + breite, this.height - dicke,
					Color.WHITE));
		}
		// Rechts
		for (int i = 0; i <= height; i += breite) {
			addMove(new Schritt(this.width, i, this.width - dicke, i,
					this.width, i + breite, this.width - dicke, i + breite,
					Color.WHITE));
		}
	}

	public void addMove(Schritt s) {
		this.schritte.add(s);
	}

	public Iterable<Schritt> getSchritte() {
		return schritte.get();
	}

	public Iterable<Schritt> getSchritteInRect(Rectangle rect) {
		return schritte.getSchritteInRect(rect);
	}

	public boolean collides(Schritt s) {
		for (Schritt schritt : schritte.getSchritteUm(s)) {
			if (schritt.overlapsWith(s)) {
				return true;
			}
		}
		return false;
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}

	public boolean isOutOfFeld(Schritt schritt) {
		if (schritt.getFinalX() < 0 || schritt.getFinalX() > this.width)
			return true;
		if (schritt.getFinalY() < 0 || schritt.getFinalY() > this.height)
			return true;
		return false;
	}

	public void removeCircle(final int x, final int y, final int radius) {
		schritte.remove(new SchrittList.Filter<Schritt>() {
			@Override
			public boolean apply(Schritt type) {
				return type.overlapsWith(x - width, y - height, radius)
						|| type.overlapsWith(x, y - height, radius)
						|| type.overlapsWith(x + width, y - height, radius)
						|| type.overlapsWith(x - width, y, radius)
						|| type.overlapsWith(x, y, radius)
						|| type.overlapsWith(x + width, y, radius)
						|| type.overlapsWith(x - width, y + height, radius)
						|| type.overlapsWith(x, y + height, radius)
						|| type.overlapsWith(x + width, y + height, radius);
			}
		});
	}
}
